/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(function(){
    
    /*  HOME  */
    var cache = {},
    lastXhr;
    
    $("#origin-search").autocomplete({"url":"SearchRoundByOrigin","minLength":"0","source":function(request,response){
			term = request.term;
			if ( term in cache ) {
				response( cache[ term ] );
				return;
			}
			lastXhr = $.getJSON( "SearchRoundByOrigin", request, function( data, status, xhr ) {
				cache[ term ] = data;
				if ( xhr === lastXhr ) {
					response( data );
					
				}
			});	
		},"change":function( event, ui ) {
			
			if ( !ui.item ) {
				var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ), valid = false;
				for(item in cache[term]){
					if(cache[term][item].match(matcher)){
						valid = true;
						break;
					}
				}
				if ( !valid ) {
					$( this ).val( "" );
					input.data( "autocomplete" ).term = "";
					return false;
				}
			}
		}});
				
     
     $("#destiny-search").autocomplete({"url":"SearchRoundByDestiny","minLength":"0","source":function(request,response){
			term = request.term;
			if ( term in cache ) {
				response( cache[ term ] );
				return;
			}
			lastXhr = $.getJSON( "SearchRoundByDestiny", request, function( data, status, xhr ) {
				cache[ term ] = data;
				if ( xhr === lastXhr ) {
					response( data );
					
				}
			});	
		},"change":function( event, ui ) {
			
			if ( !ui.item ) {
				var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ), valid = false;
				for(item in cache[term]){
					if(cache[term][item].match(matcher)){
						valid = true;
						break;
					}
				}
				if ( !valid ) {
					$( this ).val( "" );
					input.data( "autocomplete" ).term = "";
					return false;
				}
			}
		}});
   
  
    
    
    
    
    /*ALTA DE RONDAS*/
    $("#createRoundButton").click(function(){
            location.href = "newRound.jsp";
    });
    
    
       
    /*RESULTADO DE LA BUSQUEDA DE RONDAS PUBLICAS*/
    $("#mapDetails").dialog({
        autoOpen: false,
        modal:true,
        width:"600px"
    });
    
    $(".originDestinyDetails").click(function(){
       $("#mapDetails").dialog("open");
    });
    
    
//    $(".ratingRadio").change(function(){
//        var name = $(this).attr("name");
//        var name = name.split("-");
//        
//        var roundId = name[1];
//        var value = $(this).val();
//        var owner = $(this).attr("owner");
//        
//        
//        $.ajax({
//            url: 'ajax/test.html',
//            success: function(data) {
//                $('.result').html(data);
//                alert('Load was performed.');
//            }
//        });
//        
//        $.ajax({
//            type: "POST",
//            url: "RateOwner",
//            data: { roundId: roundId, value: value ,owner:owner },
//            success:function(data)
//            {
//                alert( "Data :" + data );
//            }
//        });
//        
//        
//        
//    });
    
});


$(document).ready(function(){
     /*     HOME     */
      $("#hour1").AnyTime_picker(
      {format: "%H:%i", labelTitle: "Hora",
        labelHour: "Hora", labelMinute: "Minuto"} );
    
     
    
    
});
